import { login } from "@/api/epgms/account/login";
import { register } from "@/api/epgms/account/register";
import { getUserInfo } from "@/api/epgms/account/getUserInfo";
import filterAsyncRoutes from "@/core/addRoutePermission";
import {
  setToken,
  getToken,
  setUserName,
  getUserName,
  removeUserName,
  removeToken,
  getRole,
  setRole,
  removeRole,
  getMobile,
  setMobile,
  removeMobile
} from "@/core/auth";
import {asyncRoutes, router} from "@/router";

const state = {
  token: "",
  username: "",
  account: "",
  sidebar: false,
  routes: [],
  roles: [],
  id: ""
};

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, username) => {
    state.username = username;
  },
  SET_ACCOUNT: (state, account) => {
    state.account = account;
  },
  SET_SIDEBAR: (state) => {
    state.sidebar = !state.sidebar;
  },
  SET_ROUTES: (state, routes) => {
    state.routes = routes;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_ID: (state, id) => {
    state.id = id;
  },
};

const actions = {
  // 登录
  async LoginAction(context, userInfo) {
    const { account, password } = userInfo;
    var result = await login({
      username: account.trim(),
      password: password.trim(),
    });
    const { data } = result;
    const { token, user_info } = data;
    context.commit("SET_TOKEN", token);
    const { username, roles, mobile, id } = user_info;
    console.log(data)
    console.log(user_info)
    context.commit("SET_NAME", username);
    context.commit("SET_ACCOUNT", mobile);
    context.commit("SET_ROLES", roles);
    context.commit("SET_ID", id);
  },

  async RegisterAction(context, userInfo) {
    const { account, password } = userInfo;
    var result = await register({
      username: account.trim(),
      password: password.trim(),
    });
    const { data } = result;
    const { token, user_info } = data;
    context.commit("SET_TOKEN", token);
    const { username, roles, mobile, id } = user_info;
    context.commit("SET_NAME", username);
    context.commit("SET_ACCOUNT", mobile);
    context.commit("SET_ROLES", roles);
    context.commit("SET_ID", id);
  },

  // 获取用户信息
  async GetUserInfo(context) {
    // const result = await getUserInfo(token);
    // console.log(result)
    // const { data } = result;
    // const { roles } = data;
    // return roles;
    return context.state.roles
  },

  // 退出登录
  async Logout(context) {
    console.log("sss")
    context.commit("SET_TOKEN", "");
    context.commit("SET_ID", "");
    context.commit("SET_NAME", "");
    context.commit("SET_ACCOUNT", "");
    context.commit("SET_ROLES", []);
  },

  // sidebar status
  ChangeSidebar(context) {
    context.commit("SET_SIDEBAR");
  },

  async GenerateRoutes(context, roles) {
    const accessedRoutes = await filterAsyncRoutes(roles);
    context.commit("SET_ROUTES", accessedRoutes);
    return accessedRoutes;
  },
};

export default {
  namespace: true,
  state,
  mutations,
  actions,
};
